/**
 * futures : masterlink.futures.dao.RealTimeRateTXODao.java
 */
package masterlink.futures.dao;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import baytony.util.Util;

import masterlink.futures.vo.RealTimeRateTXO;
import masterlink.futures.vo.RealTimeRateTXO.RealTimeRateTXOID;

/**
 * TODO Write the description
 *
 * @author : tonyyang
 * Create date : 2011/11/4
 * @version 1.0.0	
 */
@Repository
@Qualifier("RealTimeRateTXODao")
public class RealTimeRateTXODao extends TimesTenHibernateDao<RealTimeRateTXO, RealTimeRateTXOID> {
	
	private static final SimpleDateFormat sd=new SimpleDateFormat("yyyyMM");
	private static final String FIND_ALL="from RealTimeRateTXO where id.rate=1";
	private static final String FIND_MONTH="SELECT distinct setMonth from RealTimeRateTXO where id.rate=1 and setMonth>=? order by setMonth";
	private static final String FIND_PRODID="SELECT id.prodId from RealTimeRateTXO where id.rate=1";
	
	@SuppressWarnings("unchecked")
	@Override
	public List<RealTimeRateTXO> findAll() {
		return (List<RealTimeRateTXO>) super.findByHql(FIND_ALL);
	}
	
	@SuppressWarnings("unchecked")
	public List<RealTimeRateTXO> findAll(String month) {
		if(!Util.isEmptyString(month)){
			return (List<RealTimeRateTXO>) super.findByHql(FIND_ALL+" and setMonth=?",month);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<RealTimeRateTXO> findByProdId(String pid){
		if(!Util.isEmptyString(pid)){
			StringBuffer st=new StringBuffer(FIND_ALL);
			st.append(" and id.prodId=?");
			return (List<RealTimeRateTXO>) super.findByHql(st.toString(),pid);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<String> findMonth(){
		return (List<String>) super.findByHql(FIND_MONTH,sd.format(new Date()));
	}
	
	@SuppressWarnings("unchecked")
	public List<String> findProdid(String term){
		if(!Util.isEmptyString(term)){
			StringBuffer st=new StringBuffer(FIND_PRODID);
			st.append(" and (lower(id.prodId) like '%"+term.toLowerCase()+"%')");
			return (List<String>) super.findByHql(st.toString());
		}
		return null;
	}
	
}
